package com.mao.interview.number;

/**
 * 给定一个数值n，求1到n所有数字中，出现1个的个数，例如f(13)=6,含1的数字为1,10,11,12,13,共6个
 *
 */
public class CountsOfOneParser {

	public static long parse(int a) {

		int i = a;
		long count = 0;
		while (i > 0) {
			count += numberOfOne(i);
			i--;
		}
		return count;
	}

	private static int numberOfOne(int a) {

		String strNumber;
		strNumber = String.valueOf(a);

		char[] charArray = strNumber.toCharArray();

		int count = 0;
		for (int i = 0; i < charArray.length; i++) {
			if (charArray[i] == '1') {
				count++;
			}
		}
		return count;
	}

	public static void main(String[] args) {

		System.out.println(parse(100));
	}
}
